package fr.utbm.gl52.smartgrid.view.listview;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.lang.ref.WeakReference;
import java.util.LinkedList;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import fr.utbm.gl52.smartgrid.model.device.IDevice;
import fr.utbm.gl52.smartgrid.util.BiControllable;
import fr.utbm.gl52.smartgrid.util.Constants;

public class SubListPanel extends JPanel implements BiControllable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8200217366728668860L;
	private JPanel title; // displays the title and "Add" button
	private JPanel list; // displays the list of DeviceViews
	private JLabel name;
	private JButton addButton;
	private String classType;
	WeakReference<ActionListener> internalController;
	WeakReference<ActionListener> externalController;

	private LinkedList<DeviceView> deviceViews; // displayed devices

	public SubListPanel(String _name, String classTypeStr) {
		super();

		this.classType = classTypeStr;

		this.deviceViews = new LinkedList<DeviceView>();

		this.name = new JLabel(_name);
		this.addButton = new JButton(Messages.getString("SubListPanel.0")); //$NON-NLS-1$
		this.addButton.setActionCommand(Constants.PREFIX_CREATE + classTypeStr);

		this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

		this.list = new JPanel();
		this.list.setLayout(new BoxLayout(this.list, BoxLayout.PAGE_AXIS));

		this.title = new JPanel();
		this.title.setLayout(new FlowLayout());
		this.title.add(this.name, FlowLayout.LEFT);
		this.title.add(this.addButton, FlowLayout.CENTER); // FIXME
		this.title.setBorder(BorderFactory.createLineBorder(Color.black));
		this.title.setMaximumSize(new Dimension(300, 40));

		this.add(this.title);
		this.add(this.list);
	}

	public void addDevice(IDevice device) {
		DeviceView dView = new DeviceView(device);
		dView.addController(this.externalController.get());
		this.deviceViews.push(dView);
	}

	public void removeDevice(IDevice device) {
		this.deviceViews.remove(device);
	}

	public void update() {
		for (DeviceView dView : this.deviceViews) {
			this.list.add(dView);
		}
		this.list.revalidate();
	}

	public void addController(ActionListener controller) {
		this.addButton.addActionListener(controller);

		for (DeviceView deviceView : this.deviceViews) {
			deviceView.addController(controller);
		}
	}

	public void addControllers(ActionListener _internalController,
			ActionListener _externalController) {
		this.internalController = new WeakReference<ActionListener>(
				_internalController);
		this.externalController = new WeakReference<ActionListener>(
				_externalController);
		this.addButton.addActionListener(_internalController);
		for (DeviceView deviceView : this.deviceViews) {
			deviceView.addController(_externalController);
		}
	}

	public String getClassType() {
		return this.classType;
	}

	public void setClassType(String classType) {
		this.classType = classType;
	}

	public void reset() {
		// TODO Auto-generated method stub
		this.deviceViews.clear();
		this.list.removeAll();
		this.list.revalidate();
		this.list.repaint();
	}

}
